// pages/cardDetails/cardDetails.js
const innerAudioContextRecord = wx.createInnerAudioContext();  //创建录音音频对象
const innerAudioContext = wx.createInnerAudioContext();  //创建音频对象
innerAudioContextRecord.autoplay = true;
innerAudioContext.loop = true;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    musicStatus: false,  //音乐是否播放
    share: true,
    avatar: '',
    nickName: '',
    month: '',
    day: '',
    open:false,
    packet:false,   //红包弹窗状态
    bgimg:'',
  },
  // musicHandle
  musicHandle(e) {
    let that = this;
    let musicStatus = that.data.musicStatus;
    if (musicStatus) {
      innerAudioContext.pause();
      innerAudioContext.onPause(() => {
        console.log('暂停播放');
        that.setData({
          musicStatus: false
        })
      })
    } else {
      innerAudioContext.play();
      innerAudioContext.onPlay(() => {
        console.log('开始播放');
        that.setData({
          musicStatus: true
        })
      })
    }
  },
  // 播放音频
  audioCard: function () {
    let that = this;
    that.setData({
      musicStatus: false
    }, () => {
      innerAudioContext.stop()
      innerAudioContextRecord.play()
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    var requestdata = wx.getStorageSync('requestdata');
    var bgimg = wx.getStorageSync('bgimg');
    console.log(requestdata)

    var content = requestdata.greet.split("@#$");
    console.log(content)
    if (content[0]) {
      requestdata.greet = content
    } else {
      requestdata.greet = content[0]
    }

    that.setData({
      info:requestdata,
      bgimg: bgimg
    })
    console.log(that.data.info.greet);
    innerAudioContext.src = requestdata.musicpath; // 设置音频地址
    innerAudioContextRecord.src = requestdata.audiopath; // 设置音频地址
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
    if (innerAudioContextRecord.src) {
      that.setData({
        hasaudio: false
      })
      innerAudioContextRecord.onPlay(() => {
        console.log('录音开始播放')
      })
      innerAudioContextRecord.onError((res) => {
        console.log(res.errMsg)
        console.log(res.errCode)
      })
      innerAudioContextRecord.onEnded((res) => {
        console.log('录音播放结束')
        that.setData({
          musicStatus: true
        }, () => { innerAudioContext.play() })
      })
    } else {
      that.setData({
        musicStatus: true,
        hasaudio: false
      }, () => { innerAudioContext.play() })
    }
    // 获取系统时间
    var myDate = new Date();
    let monthNew = myDate.getMonth()
    let dayNew = myDate.getDate()
    var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    that.setData({
      month: month[monthNew],
      day: dayNew
    })
    if(requestdata.money>0){
      // 10秒之后弹出红包
      setTimeout(function () {
        that.setData({
          packet: true
        })
      }, 12000)
    }
  },
  // 返回
  indexHandle: function () {
    innerAudioContext.stop()
    wx.navigateBack({
      changed:true
    })
  },
  // 发送朋友
  sendFriends: function () {
    innerAudioContext.stop()
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  // 点击阴影
  closeHandle: function () {
    let share = this.data.share
    this.setData({
      share: true
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  // 生成卡片
  postersHandle: function () {
    let share = this.data.share
    this.setData({
      share: true
    })
    wx.navigateTo({
      url: '/pages/posters/posters',
    })
  },
  //开红包
  openHandle: function () {
    let that  = this
    let { open } = that.data
    that.setData({
      open:true
    })
  },
  //关闭红包弹窗
  closePacket: function () {
    let that = this
    let { packet } = that.data
    that.setData({
      packet: false
    })
  },
  // 查看红包详情
  packetList: function () {
    wx.navigateTo({
      url: '/pages/packetList/packetList',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    innerAudioContext.stop()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    innerAudioContext.stop()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  //   let share = this.data.share
  //   this.setData({
  //     share: true
  //   })
  // }
})